import { initWebSocket, wsSend } from './trade'
import store from '../store'
import { Notification } from 'element-ui';

export function getTradeOnMessage (dataArr) {
    let closeFlag = 0;
    let nowLang = store.getters.lang;
    let nofityTitle = '成功';
    let errTitle = '提示';
    let notifyHtml = "";

    if (nowLang == 'en') {
        nofityTitle = 'success';
        errTitle = 'tip'
    }

    switch (dataArr[0]) {
        case '4':
            // 用户实时资金更新(4)(高频接收)
            if (dataArr[2] !== 'a') {
                // console.log(dataArr)
                store.dispatch('product/updateMoneyLIST', dataArr)
            }
            break;
        case '5':
            if (dataArr[2] !== 'a') {
                // 用户资金接收，（低频接收）
                // console.log(dataArr)
                store.dispatch('product/updateLowCurList', dataArr)
            }
            break;
        case '11':
            // 用户修改密码（11）
            store.dispatch("bar/changeLoadings", { status: false });

            let resetLogin = '密码修改成功，请重新登录';

            if (nowLang == 'hk') {
                resetLogin = '密碼修改成功,請重新登錄';
            }

            if (nowLang == 'en') {
                resetLogin = 'The password has been modified successfully. Please log in again.';
            }

            if (dataArr[1] === '1') {
                // 修改成功
                store.dispatch("bar/switchAlert", {
                    title: nofityTitle,
                    msg: resetLogin,
                    type: 1
                });
                store.dispatch('user/changePwStu', true)
                window.setTimeout(() => {
                    store.dispatch('user/logout', true)
                }, 500)
            } else if (dataArr[1] === '0') {
                store.dispatch("bar/switchAlert", {
                    msg: dataArr[2],
                    type: 3
                })
                store('user/changePwStu', false)
            }
            break;
        case '15':
            // console.timeEnd("建仓操作服务器响应耗时："); ///266
            // store.dispatch("bar/changeLoadings", { status: false });
            // console.log(dataArr)
            let bsDirection = dataArr[6] == '1' ? '买' : '卖';

            if (nowLang == 'zh') {
                notifyHtml = `<div class="trade-notifica"><div><span>委托数量：${dataArr[4]}</span><span>委托方向：${bsDirection}</span></div><div><span>委托价格：${dataArr[5] === '0' ? '市价' : dataArr[5]}</span><span>委托合约：${store.getters.selName}</span></div></div>`
            }

            if (nowLang == 'hk') {
                notifyHtml = `<div class="trade-notifica"><div><span>委托數量：${dataArr[4]}</span><span>委托方向：${bsDirection}</span></div><div><span>委托價格：${dataArr[5] === '0' ? '市價' : dataArr[5]}</span><span>委托合約：${store.getters.selName}</span></div></div>`
            }

            if (nowLang == 'en') {
                notifyHtml = `<div class="trade-notifica"><div><span>numberOfOrders：${dataArr[4]}</span><span>commissionDirection：${bsDirection}</span></div><div><span>commissionPrice：${dataArr[5] === '0' ? 'MarketPrice' : dataArr[5]}</span><span>commissionContract：${store.getters.selName}</span></div></div>`
            }

            if (dataArr[1] === '1') {
                store.dispatch("panel/playTipsAudio", 2);
                Notification({
                    title: nofityTitle,
                    position: 'bottom-right',
                    dangerouslyUseHTMLString: true,
                    message: notifyHtml,
                    offset: 20,
                    type: 'success'
                });
                // console.timeEnd("建仓操作提示完毕耗时："); // 335
            } else if (dataArr[1] === '0') {
                store.dispatch("panel/playTipsAudio", 1);

                Notification({
                    title: errTitle,
                    position: 'bottom-right',
                    message: dataArr[2],
                    offset: 20,
                    type: 'warning'
                });
            }

            store.commit('product/SET_MINILOADING', false)
            break;
        case '16':
            store.dispatch("bar/changeLoadings", { status: false });
            let cancelObj = store.getters.commissionlist.find(v => v.ID === dataArr[2]);
            // console.log('当前撤单合约：', cancelObj)
            let withdrawal = '已经成功撤单'; // has successfully withdrawn the order

            if (nowLang == 'hk') {
                withdrawal = '已經成功撤單';
            }

            if (nowLang == 'en') {
                withdrawal = 'has successfully withdrawn the order';
            }

            if (dataArr[1] !== '0') {
                Notification({
                    title: nofityTitle,
                    position: 'bottom-right',
                    message: cancelObj.Name + withdrawal,
                    offset: 20,
                    type: 'success'
                });
            } else if (dataArr[1] === '0') {
                store.dispatch("panel/playTipsAudio", 1);

                Notification({
                    title: errTitle,
                    position: 'bottom-right',
                    message: dataArr[2],
                    offset: 20,
                    type: 'warning'
                });
            }
            break
        case '17':
            closeFlag = 0;
            store.commit('product/SET_MINILOADING', false)
            store.dispatch("bar/changeLoadings", { status: false });
            // console.log(dataArr)

            // 17;1;委托卖出记录ID;期货合约合约ID;数量;价格;买卖方向;
            let bsDirection1 = dataArr[6] == '1' ? '买' : '卖';

            if (nowLang == 'zh') {
                notifyHtml = `<div class="trade-notifica"><div><span>委托数量：${dataArr[4]}</span><span>委托方向：${bsDirection1}</span></div><div><span>委托价格：${dataArr[5] === '0' ? '市价' : dataArr[5]}</span><span>委托合约：${store.getters.selName}</span></div></div>`
            }

            if (nowLang == 'hk') {
                notifyHtml = `<div class="trade-notifica"><div><span>委托數量：${dataArr[4]}</span><span>委托方向：${bsDirection1}</span></div><div><span>委托價格：${dataArr[5] === '0' ? '市價' : dataArr[5]}</span><span>委托合約：${store.getters.selName}</span></div></div>`
            }

            if (nowLang == 'en') {
                notifyHtml = `<div class="trade-notifica"><div><span>numberOfOrders：${dataArr[4]}</span><span>commissionDirection：${bsDirection1}</span></div><div><span>commissionPrice：${dataArr[5] === '0' ? 'MarketPrice' : dataArr[5]}</span><span>commissionContract：${store.getters.selName}</span></div></div>`
            }

            if (dataArr[1] === '1') {
                Notification({
                    title: nofityTitle,
                    position: 'bottom-right',
                    dangerouslyUseHTMLString: true,
                    message: notifyHtml,
                    offset: 20,
                    type: 'success'
                });
            }

            if (dataArr[1] == '0') {
                store.dispatch("panel/playTipsAudio", 1);
                closeFlag = 1;

                Notification({
                    title: errTitle,
                    position: 'bottom-right',
                    message: dataArr[2],
                    offset: 20,
                    type: 'warning'
                });
            }
            break;
        case '20':
            // 获取合约代码信息(20)
            if (dataArr[1] === '1' || dataArr[1] === '5') {
                store.dispatch('product/updateProductList', dataArr)
            } else if (dataArr[1] === '0') {
                console.log('获取合约代码信息错误信息：', dataArr[2])
            }
            break;
        case '22':
            // 获取货币汇率信息（22）
            if (dataArr[1] === '1' || dataArr[1] === '5') {
                store.dispatch('product/updateParities', dataArr)
            } else if (dataArr[1] === '0') {
                console.log('获取货币汇率错误信息：', dataArr[2])
            }
            break;
        case '23':
            // 获取到持仓单信息(23)
            if (dataArr[1] === '1' || dataArr[1] === '5') {
                store.dispatch('panel/updatePositionBook', dataArr)
            } else if (dataArr[1] === '0') {
                console.log('获取持仓单错误信息：', dataArr[2])
            } else if (dataArr[1] === '2' || dataArr[1] === '3' || dataArr[1] === '4') {
                // 增加/ 删除
                // console.log(dataArr)
                store.dispatch('panel/holdAPosition', dataArr)
            }
            break;
        case '24':
            // 获取到委托单信息(24)
            if (dataArr[1] === '1' || dataArr[1] === '5') {
                store.dispatch('panel/updateOrderTicket', dataArr)
            } else if (dataArr[1] === '0') {
                console.log('获取委托单错误信息：', dataArr[2])
            } else if (dataArr[1] === '2' || dataArr[1] === '3' || dataArr[1] === '4') {
                // 增加/ 删除
                // console.log(dataArr)
                store.dispatch('panel/holdCommission', dataArr)
            }
            break;
        case '25':
            // 获取到成交明细信息(25)
            if (dataArr[1] === '1' || dataArr[1] === '2' || dataArr[1] === '3' || dataArr[1] === '4' || dataArr[1] === '5') {
                store.dispatch('panel/updateTransactionDetails', dataArr)
            } else if (dataArr[1] === '0') {
                console.log('获取成交明细错误信息：', dataArr[2])
            }
            break;
        case '26':
            if (dataArr[1] === '1' || dataArr[1] === '5') {
                store.dispatch('product/updateLeaderboard', dataArr)
            } else if (dataArr[1] === '0') {
                console.log('获取排行榜错误信息：', dataArr[2])
            }
            break;
        case '27':
            store.dispatch("bar/changeLoadings", { status: false });
            let orderSucc = '跟单成功';
            let modifysucc = '修改成功';
            let opeSuccess = '操作成功';

            if (nowLang == 'hk') {
                orderSucc = '跟單成功';
            }

            if (nowLang == 'en') {
                orderSucc = 'Successful with the order';
                modifysucc = 'Successfully modified';
                opeSuccess = 'Successful operation';
            }
            // console.log(dataArr)
            // 27;0;错误提示;
            if (dataArr[1] === '0') {
                store.dispatch("panel/playTipsAudio", 1);
                Notification({
                    title: errTitle,
                    position: 'bottom-right',
                    message: dataArr[2],
                    offset: 20,
                    type: 'warning'
                });
            }

            // 跟单记录新增返回 27;1;1;跟单ID号;用户ID;用户名;合约ID;操作类别;数量;跟单类别;超价倍数;数量类别;  
            if (dataArr[1] === '1' && dataArr[2] === '1') {
                Notification({
                    title: nofityTitle,
                    position: 'bottom-right',
                    message: orderSucc,
                    offset: 20,
                    type: 'success'
                });
                wsSend('28;')
            }

            // 修改跟单记录返回 27;1;2;跟单ID号;用户ID;用户名;合约ID;操作类别;数量;跟单类别;超价倍数;数量类别;
            if (dataArr[1] === '1' && dataArr[2] === '2') {
                Notification({
                    title: nofityTitle,
                    position: 'bottom-right',
                    message: modifysucc,
                    offset: 20,
                    type: 'success'
                });
                wsSend('28;')
            }

            let opeSuccess1 = '操作成功';

            if (nowLang == 'en') {
                opeSuccess1 = 'Successful operation';
            }

            // 跟单取消记录返回
            if (dataArr[1] === '1' && dataArr[2] === '3') {
                // 返回（取消）：27;0;错误提示;
                // 27;1;3;跟单ID号;描述;  
                let tipsMsg = dataArr[4] === '' ? opeSuccess1 : dataArr[4];
                Notification({
                    title: nofityTitle,
                    position: 'bottom-right',
                    message: tipsMsg,
                    offset: 20,
                    type: 'success'
                });
                wsSend('28;')
            }
            break;
        case '28':
            if (dataArr[1] === '1') {
                store.dispatch('product/updateDocumentary', dataArr)
            } else if (dataArr[1] === '0') {
                console.log('获取跟单列表错误信息：', dataArr[2])
            }
            break;
        case '29':
            // 全部平仓
            store.dispatch("bar/changeLoadings", { status: false });

            // console.log(dataArr)
            // if (dataArr[1] === '1' && !closeFlag) {
            //     let allConfully = "合约已经全部成交" // 
            //     if (nowLang == 'hk') {
            //         allConfully = "合約已經全部成交"
            //     }
            //     if (nowLang == 'en') {
            //         allConfully = "The contract has been fully filled"
            //     }

            //     Notification({
            //         title: nofityTitle,
            //         position: 'bottom-right',
            //         message: allConfully,
            //         offset: 20,
            //         type: 'success'
            //     });
            // } else 
            if (dataArr[1] === '0') {
                store.dispatch("panel/playTipsAudio", 1);
                Notification({
                    title: errTitle,
                    position: 'bottom-right',
                    message: dataArr[2],
                    offset: 20,
                    type: 'warning'
                });
            }
            break
        case '30':
            store.dispatch("bar/changeLoadings", { status: false });
            // console.log(dataArr)

            if (dataArr[1] === '1') {
                let allWithdrawn = "合约已经全部撤单" // 
                if (nowLang == 'hk') {
                    allWithdrawn = "合約已經全部撤單"
                }
                if (nowLang == 'en') {
                    allWithdrawn = "The contract has been fully withdrawn"
                }

                Notification({
                    title: nofityTitle,
                    position: 'bottom-right',
                    message: allWithdrawn,
                    offset: 20,
                    type: 'success'
                });
            } else if (dataArr[1] === '0') {
                store.dispatch("panel/playTipsAudio", 1);
                Notification({
                    title: errTitle,
                    position: 'bottom-right',
                    message: dataArr[2],
                    offset: 20,
                    type: 'warning'
                });
            }
            break
        case '31':
            // 返回：31;0;错误提示;
            // 31;1;描述;
            console.log(dataArr)
            store.dispatch("bar/changeLoadings", { status: false });
            if (dataArr[1] === '0') {
                store.dispatch("panel/playTipsAudio", 1);
                Notification({
                    title: errTitle,
                    position: 'bottom-right',
                    message: dataArr[2],
                    offset: 20,
                    type: 'warning'
                });
            }

            if (dataArr[1] === '1') {
                let tipsMsg = dataArr[2] === '' ? opeSuccess : dataArr[2];
                Notification({
                    title: nofityTitle,
                    position: 'bottom-right',
                    message: tipsMsg,
                    offset: 20,
                    type: 'success'
                });
                wsSend('28;')
            }
            break;
        case '32':
            store.dispatch("bar/changeLoadings", { status: false });
            // console.log(dataArr)
            // ["32", "1", "823822", "521", "1", "0.76275", "1", ""]
            if (dataArr[1] !== '0') {

                let bsDirection2 = dataArr[6] == '1' ? '买' : '卖';

                if (nowLang == 'zh') {
                    notifyHtml = `<div class="trade-notifica"><div><span>委托数量：${dataArr[4]}</span><span>委托方向：${bsDirection2}</span></div><div><span>委托价格：市价</span><span>委托合约：${store.getters.selName}</span></div></div>`
                }

                if (nowLang == 'hk') {
                    notifyHtml = `<div class="trade-notifica"><div><span>委托數量：${dataArr[4]}</span><span>委托方向：${bsDirection2}</span></div><div><span>委托價格：市價</span><span>委托合約：${store.getters.selName}</span></div></div>`
                }

                if (nowLang == 'en') {
                    notifyHtml = `<div class="trade-notifica"><div><span>numberOfOrders：${dataArr[4]}</span><span>commissionDirection：${bsDirection2}</span></div><div><span>commissionPrice：MarketPrice</span><span>commissionContract：${store.getters.selName}</span></div></div>`
                }
                let backhand = '反手成功';

                if (nowLang == 'en') {
                    backhand = 'Backhand success';
                }

                Notification({
                    title: backhand,
                    position: 'bottom-right',
                    dangerouslyUseHTMLString: true,
                    message: notifyHtml,
                    offset: 20,
                    type: 'success'
                });
            } else if (dataArr[1] === '0') {
                store.dispatch("panel/playTipsAudio", 1);
                Notification({
                    title: errTitle,
                    position: 'bottom-right',
                    message: dataArr[2],
                    offset: 20,
                    type: 'warning'
                });
            }
            break;
        case '33':
            store.dispatch("bar/changeLoadings", { status: false });
            // console.log(dataArr)
            let conversionTip = '货币转换成功';
            if (nowLang == 'hk') {
                conversionTip = "貨幣轉換成功"
            }
            if (nowLang == 'en') {
                conversionTip = "Successful currency conversion"
            }
            if (dataArr[1] !== '0') {
                Notification({
                    title: nofityTitle,
                    position: 'bottom-right',
                    message: conversionTip,
                    offset: 20,
                    type: 'success'
                });
            } else if (dataArr[1] === '0') {
                store.dispatch("panel/playTipsAudio", 1);
                Notification({
                    title: errTitle,
                    position: 'bottom-right',
                    message: dataArr[2],
                    offset: 20,
                    type: 'warning'
                });
            }
            break;
        case '36':
            if (dataArr[1] === '1' || dataArr[1] === '5') {
                // 获取信息列表循环
                store.dispatch('user/updateMsgList', dataArr)
            }
            if (dataArr[1] === '0') {
                console.log('获取信息列表错误：' + dataArr[2])
            }
            break;
        case '37':
            store.dispatch("bar/changeLoadings", { status: false });
            // console.log(dataArr)
            // 修改成功（持仓数量等于0的，删除止盈止损的修改）
            // dataArr[4]: 持仓数量，当为0时说明当前是删除止盈止损
            let delStu = store.getters.isPLDel; // 当前用户删除状态 true说明用户删除
            let delMsg = "止盈止损刪除成功！";
            let changeMsg = '止盈止损修改成功！'

            if (nowLang == 'hk') {
                delMsg = "止盈止損刪除成功！";
                changeMsg = '止盈止損修改成功！'
            }
            if (nowLang == 'en') {
                delMsg = "Stop Loss and Loss is deleted successfully!";
                changeMsg = 'Stop profit stop loss modified successfully!'
            }

            if (dataArr[1] === '1' && dataArr[4] === '0' && delStu) {
                Notification({
                    title: nofityTitle,
                    position: 'bottom-right',
                    message: delMsg,
                    offset: 20,
                    type: 'success'
                });

                wsSend('23;')  // 更新持仓单列表
            }

            // 修改成功（持仓数量大于0的，正常修改）
            if (dataArr[1] === '1' && dataArr[4] !== '0') {
                Notification({
                    title: nofityTitle,
                    position: 'bottom-right',
                    message: changeMsg,
                    offset: 20,
                    type: 'success'
                });

                wsSend('23;')  // 更新持仓单列表
            }

            // 修改止盈止损错误信息
            if (dataArr[1] === '0') {
                store.dispatch("panel/playTipsAudio", 1);
                Notification({
                    title: errTitle,
                    position: 'bottom-right',
                    message: dataArr[3],
                    offset: 20,
                    type: 'warning'
                });
            }
            break;
        case '38':
            // 获取止盈止损（35）
            // console.log(dataArr)
            if (dataArr[1] === '1' || dataArr[1] === '5') {
                store.dispatch('panel/updateFullStop', dataArr)
            } else if (dataArr[1] === '0') {
                console.log('获取止盈止损错误信息', dataArr[2])
            }
            break;
        case '41':
            // 获取用户委托单历史记录（41）
            if (dataArr[1] === '2') store.dispatch("bar/changeLoadings", { status: false });
            if (dataArr[1] === '1' || dataArr[1] === '2') {
                // console.log(dataArr)
                store.dispatch('panel/updateHisCommission', dataArr)
            } else if (dataArr[1] === '0') {
                console.log('获取委托单历史记录错误信息：', dataArr[2])
            }
            break;
        case '42':
            // 获取用户成交历史记录（42）
            if (dataArr[1] === '2') store.dispatch("bar/changeLoadings", { status: false });
            if (dataArr[1] === '1' || dataArr[1] === '2') {
                // console.log(dataArr)
                store.dispatch('panel/updateHisTransaction', dataArr)
            } else if (dataArr[1] === '0') {
                console.log('获取委托单历史记录错误信息：', dataArr[2])
            }
            break;
        case '43':
            // 获取用户平仓历史记录（43）
            if (dataArr[1] === '2') store.dispatch("bar/changeLoadings", { status: false });
            if (dataArr[1] === '1' || dataArr[1] === '2') {
                // console.log(dataArr)
                store.dispatch('panel/updateHisClose', dataArr)
            } else if (dataArr[1] === '0') {
                console.log('获取用户平仓历史记录错误信息：', dataArr[2])
            }
            break;
        case '45':
            // 获取用户历史资金流水（45）
            if (dataArr[1] === '2') store.dispatch("bar/changeLoadings", { status: false });
            if (dataArr[1] === '1' || dataArr[1] === '2') {
                // console.log(dataArr)
                store.dispatch('panel/updateCapitalFlow', dataArr)
            } else if (dataArr[1] === '0') {
                console.log('获取历史资金流水错误信息：', dataArr[2])
            }
            break;
        case '50':
            // 心跳包返回失败（50）
            if (dataArr[0] === '0') {
                console.log('心跳包连接失败，ws断开')
                // 尝试重新连接，并自动登录
                if (/ws:\/\/jy[\d]\.\w{2,16}\.com:\d{4}$/.test(store.getters.tradeUrl)) {
                    initWebSocket(new Date().getTime(), store.getters.tradeUrl, {}, res => {
                        console.log(res)
                    })
                } else {
                    console.log('交易地址有误：' + store.getters.tradeUrl)
                }
            }
            break;
        case '51':
            // 用户重新登录
            // console.log(dataArr)
            let repeatLeft = '您的账号在'
            let repeatRight = '登录，如非本人登录请注意修改密码。'

            if (nowLang == 'hk') {
                repeatLeft = '您的賬號在'
                repeatRight = '登錄，如非本人登錄請註意修改密碼。'
            }

            if (nowLang == 'en') {
                repeatLeft = 'Your account is logged in at '
                repeatRight = ', If you are not logged in, please change your password.'
            }

            // 退出登录
            store.dispatch('user/logout')
            // 提示退出登录原因
            store.dispatch("bar/switchAlert", {
                title: errTitle,
                msg: repeatLeft + dataArr[1] + repeatRight,
                type: 1
            });
            break;
        case '61':
            store.dispatch("bar/changeLoadings", { status: false });
            // console.log(dataArr)
            if (dataArr[1] === '0') {
                store.dispatch("panel/playTipsAudio", 1);
                Notification({
                    title: errTitle,
                    position: 'bottom-right',
                    message: dataArr[3],
                    offset: 20,
                    type: 'warning'
                });
            }
            break;
        case '63':
            store.dispatch("bar/changeLoadings", { status: false });
            // console.log(dataArr)
            if (dataArr[1] === '0') {
                store.dispatch("panel/playTipsAudio", 1);
                Notification({
                    title: errTitle,
                    position: 'bottom-right',
                    message: dataArr[2] + '：' + dataArr[3],
                    offset: 20,
                    type: 'warning'
                });
            }
            break;
        case '65':
            store.dispatch("bar/changeLoadings", { status: false });
            // console.log(dataArr)
            if (dataArr[1] === '0') {
                store.dispatch("panel/playTipsAudio", 1);
                Notification({
                    title: errTitle,
                    position: 'bottom-right',
                    message: dataArr[3],
                    offset: 20,
                    type: 'warning'
                });
            }
            break;
    }
}